package com.kyzh.core.pager.me.user


import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.impls.UserImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import kotlinx.android.synthetic.main.fragment_un_bind.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jetbrains.anko.support.v4.runOnUiThread
import org.jetbrains.anko.support.v4.toast

/**
 * A simple [Fragment] subclass.
 */
class UnBindFragment(val isPhone: Boolean) : BaseFragment(), ResultListener {

    private var time: Job? = null
    private var sessionId = ""
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_un_bind, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {
        if (isPhone) {
            titleView.setText(getString(R.string.unBindPhone))
            ivIcon.setImageResource(R.drawable.b_qq)
        } else {
            titleView.setText(getString(R.string.unBindEmail))
            ivIcon.setImageResource(R.drawable.b_email)
        }
        tvGetCode.setOnClickListener { getCode() }
        btNext.setOnClickListener { unBind() }
    }

    private fun unBind() {
        if (isPhone) {
            UserImpl.unBindPhone("", etCode.text.toString(), sessionId, this)
        } else {
            UserImpl.unBindEmail("", etCode.text.toString(), sessionId, this)
        }

        time?.cancel()
        tvGetCode.text = getString(R.string.getVerifyCode)
        tvGetCode.isClickable = true
    }


    override fun success() {
        requireActivity().finish()
    }

    override fun error(error: String) {
        toast(error)
    }
    private fun getCode() {
        if (isPhone) {
            UserRequest.getPhoneCodeByUid {
                sessionId = this
                getCodeSuccess()
            }
        } else {
            UserRequest.getEmailCodeByUid {
                sessionId = this
                getCodeSuccess()
            }
        }

    }

    private fun getCodeSuccess(){
        tvGetCode.isClickable = false
        time = GlobalScope.launch {
            repeat(60) { time ->
                val sec = 59 - time
                if (sec == 0) {
                    val msg = Message()
                    msg.what = 1
                    handler.sendMessage(msg)
                } else {
                    runOnUiThread { tvGetCode.text = "${sec}${getString(R.string.countdown)}" }
                }
                delay(1000)
            }
        }
    }
    @SuppressLint("HandlerLeak")
    private val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1 -> {
                    tvGetCode.text = getString(R.string.getVerifyCode)
                    tvGetCode.isClickable = true
                    time?.cancel()
                }
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        time?.cancel()
    }
}
